package com.greenantmall.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

public class FileUtils {
	
	public static String SDPATH = Environment.getExternalStorageDirectory()
			+ "/formats/";

	public static void saveBitmap(Bitmap bm, String picName) {

		try {
			if (!isFileExist("")) {
				//File tempf = createSDDir("");
			}
			File f = new File(SDPATH, picName + ".JPEG"); 
			if (f.exists()) {
				f.delete();
			}
			FileOutputStream out = new FileOutputStream(f);
			bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static File createSDDir(String dirName) throws IOException {
		File dir = new File(SDPATH + dirName);
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {

			System.out.println("createSDDir:" + dir.getAbsolutePath());
			System.out.println("createSDDir:" + dir.mkdir());
		}
		return dir;
	}

	public static boolean isFileExist(String fileName) {
		File file = new File(SDPATH + fileName);
		file.isFile();
		return file.exists();
	}
	
	public static void delFile(String fileName){
		File file = new File(SDPATH + fileName);
		if(file.isFile()){
			file.delete();
        }
		file.exists();
	}

	public static void deleteDir() {
		File dir = new File(SDPATH);
		if (dir == null || !dir.exists() || !dir.isDirectory())
			return;
		
		for (File file : dir.listFiles()) {
			if (file.isFile())
				file.delete(); // 删除�?有文�?
			else if (file.isDirectory())
				deleteDir(); // 递规的方式删除文件夹
		}
		dir.delete();// 删除目录本身
	}

	public static boolean fileIsExists(String path) {
		try {
			File f = new File(path);
			if (!f.exists()) {
				return false;
			}
		} catch (Exception e) {

			return false;
		}
		return true;
	}
	
	/**
     * 递归删除文件
     * @param file
     * @throws InterruptedException
     */
    public static void reverseDeleteFile(File delFile)
    {
        // 若该下载任务对应的文件不存在，则直接返回
        if (!delFile.exists())
            return;
        try
        {

            if (delFile.isDirectory())
            {
                iLog.d("******delFile.delete() delete Directory :" + delFile.getAbsolutePath());

                File[] files = delFile.listFiles();
                if (files != null)
                {
                    for (int i = 0; i < files.length; i++)
                    {
                        reverseDeleteFile(files[i]);
                    }

                }

            }

            int retryTimes = 0;
            while (retryTimes <= 2)
            {
                if (delFile.delete())
                {
                    iLog.d("******delFile.delete() exec successfully! DelteFile:" + delFile.getAbsolutePath());
                    return;
                } else
                {
                    retryTimes++;
                    iLog.d("******delFile.delete() return false! RetryTimes:"
                        + retryTimes + "**********");
                    Thread.sleep(600);
                }
            }
            iLog.d("*******delFile.delete() return false and try 3 times! Delete Failed!");
        } catch (Exception e)
        {
            e.printStackTrace();
            iLog.d("--------Exception happans in deleteDownloadFile function! Reason:"
                + e.toString());
            iLog.d("--------Quit for deleteDownloadFile------------");
        }

    }

}
